﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qianli.DirSir.Common;
using Qianli.DirSir.Model;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Qianli.DirSir.DAL
{
    public class StockhouseInfoDAL
    {
        SQLHelper helper = new SQLHelper();
        /// <summary>
        /// 显示仓库信息
        /// </summary>
        /// <returns></returns>
        public List<StorehouseInfo> Storehouseshow()
        {
            string sql = "select * from StorehouseInfo";
            DataTable table = helper.ExecDataTable(sql);
            List<StorehouseInfo> list = new List<StorehouseInfo>();
            foreach (DataRow item in table.Rows)
            {
                StorehouseInfo s = new StorehouseInfo();
                s.StorehouseID = Convert.ToInt32(item["StorehouseID"]);
                s.CityID = Convert.ToInt32(item["CityID"]);
                s.StorehouseName = item["StorehouseName"].ToString();
                s.StorehouseCoding = item["StorehouseCoding"].ToString();
                s.Streets = item["Streets"].ToString();
                s.PostCode = item["PostCode"].ToString();
                s.Contacts = item["Contacts"].ToString();
                s.FixedLine = item["FixedLine"].ToString();
                s.Phone = item["Phone"].ToString();
                s.Remarks = item["Remarks"].ToString();
                list.Add(s);
            }
            return list;
        }
        /// <summary>
        /// 添加仓库
        /// </summary>
        /// <param name="Shinfo"></param>
        /// <returns></returns>
        public int StorehouseAdd(StorehouseInfo Shinfo)
        {
            if (Shinfo.StorehouseCoding != "") 
            {
                Shinfo.StorehouseCoding = System.Guid.NewGuid().ToString();
                string sql = "insert into StorehouseInfo(StorehouseCoding,CityID,StorehouseName,Streets,Contacts,Phone) values('" + Shinfo.StorehouseCoding + "'," + Shinfo.CityID + ",'" + Shinfo.StorehouseName + "','" + Shinfo.Streets + "','" + Shinfo.Contacts + "','" + Shinfo.Phone + "')";
                return helper.ExecNonquery(sql);
            }
            else
            {
                string sql = "insert into StorehouseInfo(StorehouseCoding,CityID,StorehouseName,Streets,Contacts,Phone) values('"+Shinfo.StorehouseCoding+"'," + Shinfo.CityID + ",'" + Shinfo.StorehouseName + "','" + Shinfo.Streets + "','" + Shinfo.Contacts + "','" + Shinfo.Phone + "')";
                return helper.ExecNonquery(sql);
            }
        }
        ///// <summary>
        ///// 根据id获取仓库信息
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public StorehouseInfo StorehouseGetID(int id)
        //{
        //    string sql = "select * from StorehouseInfo where StorehouseID=" + id + "";
        //    StorehouseInfo s = new StorehouseInfo();
        //    DataTable table = helper.ExecDataTable(sql);

        //    s.StorehouseID = Convert.ToInt32(table.Rows[0]["StorehouseID"]);
        //    s.CityID = Convert.ToInt32(table.Rows[0]["CityID"]);
        //    s.StorehouseName = table.Rows[0]["StorehouseName"].ToString();
        //    s.Streets = table.Rows[0]["Streets"].ToString();
        //    s.PostCode = table.Rows[0]["PostCode"].ToString();
        //    s.Contacts = table.Rows[0]["Contacts"].ToString();
        //    s.FixedLine = table.Rows[0]["FixedLine"].ToString();
        //    s.Phone = table.Rows[0]["Phone"].ToString();
        //    s.Remarks = table.Rows[0]["Remarks"].ToString();
        //    s.Storehousestate = Convert.ToBoolean(table.Rows[0]["Storehousestate"]);

        //    return s;
        //}


        public StorehouseInfo StorehouseGetID(int id)
        {
            string sql = "select * from StorehouseInfo where StorehouseID=@id";
            StorehouseInfo s = new StorehouseInfo();

            SqlParameter[] parameters = {
                 new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value =id;


            DataTable table = helper.ExecDataTable(sql,parameters);

            s.StorehouseID = Convert.ToInt32(table.Rows[0]["StorehouseID"]);
            s.CityID = Convert.ToInt32(table.Rows[0]["CityID"]);
            s.StorehouseName = table.Rows[0]["StorehouseName"].ToString();
            s.Streets = table.Rows[0]["Streets"].ToString();
            s.PostCode = table.Rows[0]["PostCode"].ToString();
            s.Contacts = table.Rows[0]["Contacts"].ToString();
            s.FixedLine = table.Rows[0]["FixedLine"].ToString();
            s.Phone = table.Rows[0]["Phone"].ToString();
            s.Remarks = table.Rows[0]["Remarks"].ToString();  
            return s;
        }
        /// <summary>
        /// 修改仓库信息
        /// </summary>
        /// <param name="Shinfo"></param>
        /// <returns></returns>
        public int StorehouseUpdate(StorehouseInfo Shinfo)
        {
            string sql = "update StorehouseInfo set CityID=" + Shinfo.CityID + ",StorehouseName='" + Shinfo.StorehouseName + "',Streets='" + Shinfo.Streets + "',Contacts='" + Shinfo.Contacts + "',Phone='" + Shinfo.Phone + "' where StorehouseID="+Shinfo.StorehouseID+"";
            return helper.ExecNonquery(sql);
        }
        /// <summary>
        /// 根据id删除仓库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int StorehouseDelete(int id)
        {
            string sql = "delete  from StorehouseInfo where StorehouseID=" + id + "";
            return helper.ExecNonquery(sql);
        }
        /// <summary>
        /// 按条件搜索显示
        /// </summary>
        /// <param name="SthName"></param>
        /// <returns></returns>
        public List<StorehouseInfo> StorehouseInfoSelect(string SthName,string Sthcoding)
        {
            string sql = "select * from StorehouseInfo where  StorehouseName like '%" + SthName + "%' and StorehouseCoding like '%" + Sthcoding + "%'";
            DataTable table = helper.ExecDataTable(sql);
            List<StorehouseInfo> list = new List<StorehouseInfo>();
            foreach (DataRow item in table.Rows)
            {
                StorehouseInfo s = new StorehouseInfo();
                s.StorehouseID = Convert.ToInt32(item["StorehouseID"]);
                s.CityID = Convert.ToInt32(item["CityID"]);
                s.StorehouseName = item["StorehouseName"].ToString();
                s.StorehouseCoding = item["StorehouseCoding"].ToString();
                s.Streets = item["Streets"].ToString();
                s.PostCode = item["PostCode"].ToString();
                s.Contacts = item["Contacts"].ToString();
                s.FixedLine = item["FixedLine"].ToString();
                s.Phone = item["Phone"].ToString();
                s.Remarks = item["Remarks"].ToString();
                list.Add(s);
            }
            return list;
        }
    }
}
